﻿using QLKS2014.BLL;
using QLKS2014.PUBLIC;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QLKS2014.GUI.StupidContent
{
    /// <summary>
    /// thêm phòng mới hoặc cập nhật lại nếu phòng đã tồn tại
    /// </summary>
    public partial class UpdatingRoom : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }
        ObservableCollection<LoaiPhongPublic> _ocLoaiPhong;

        public ObservableCollection<LoaiPhongPublic> OcLoaiPhong
        {
            get { return _ocLoaiPhong; }
            set { _ocLoaiPhong = value;
            OnPropertyChanged("OcLoaiPhong");
            }
        }

        RoomManagement _listRoom;

        //public Boolean finish = false;
        public UpdatingRoom(RoomManagement listRoom)
        {
            InitializeComponent();
            _listRoom = listRoom;
            Loaded += UpdateRoom_Loaded;
            
        }

        void UpdateRoom_Loaded(object sender, RoutedEventArgs e)
        {
            _initialize();
        }

        private void _initialize()
        {
            txtbRoomName.Text = "";

            // init ocloaiphong
            List<LoaiPhongPublic> lLoaiPhong = new LoaiPhongBll().selectAll();
            if (lLoaiPhong != null)
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>(lLoaiPhong);
            else
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>();

        }

        private void btnUpdateRoom_Click(object sender, RoutedEventArgs e)
        {
            _updateRoom();
            
        }

        private void _updateRoom()
        {
            if (_checkUpdateRoomInput())
            {
                PhongPublic phong0 = new PhongPublic();
                phong0.TenPhong = txtbRoomName.Text;
                phong0.MaLoaiPhong = int.Parse(cbbRoomType.SelectedValue.ToString());
                phong0.GhiChu = txtbNote.Text;


                if (_isRoomNameExist(phong0))
                {
                    _updateRoom(phong0);
                }
                else
                {
                    _insertRoom(phong0);
                }

                // reset room name when finish
                txtbRoomName.Text = "";
                _listRoom.initialize();
                _initialize();
            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin!");
            }
        }

        private void _insertRoom(PhongPublic phong0)
        {
            new PhongBll().insert(phong0);
        }

        private void _updateRoom(PhongPublic phong0)
        {
            new PhongBll().update(phong0);
        }

        private bool _isRoomNameExist(PhongPublic phong)
        {
            List<PhongPublic> lPhong = new PhongBll().selectByTenPhong(phong);
            if (lPhong == null || lPhong.Count == 0)
                return false;
            return true;

        }

        private bool _checkUpdateRoomInput()
        {
            return !(String.IsNullOrEmpty(txtbRoomName.Text) || String.IsNullOrEmpty(cbbRoomType.Text));
        }

        private void UpdateRoom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                _updateRoom();
            }
        }

        private void txtbRoomName_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        internal void setRoomInfo(PhongPublic phong0)
        {
            txtbRoomName.Text = phong0.TenPhong;
            cbbRoomType.SelectedValue = phong0.MaLoaiPhong;
            cbbRoomType.Text = phong0.TenLoaiPhong;
            txtbNote.Text = phong0.GhiChu;

            //backup
            //txtbRoomName.Text = phong0.TenPhong;
            //cbbRoomType.SelectedItem = new LoaiPhongBll().selectById(phong0);
            //txtbNote.Text = phong0.GhiChu;
        }
    }
}